;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.SecondClient", [])
        .controller("SecondClientCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "$filter", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, $filter, UserContextService) {
            $scope.activeFlag = [true, false];

            $scope.dataList = [];
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.markInfo = {
                allChecked: false,
                curRow: ''
            };

            $scope.condition = {};
            $scope.reset = function () {
                $scope.condition = {};
            };
            
            $scope.tbConf = {
                fixedWidth: 100,
                scrollWidth: 1150,
                tableCol: [
                    {
                        type: "",
                        name: "企业ID",
                        width: 300,
                        dataField: "orgId",
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "公司名称",
                        width: 500,
                        dataField: "comapnyName",
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "公司简称",
                        width: 350,
                        dataField: "shortName",
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 60,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };

            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false];
                $scope.activeFlag[index] = true;
                switch (index) {
                    case 0:
                        initData();
                        break;
                    case 1:
                        break;
                    default:
                        break;
                }
            };

            function initData() {
                $scope.markInfo = {
                    allChecked: false,
                    curRow: ''
                };
                $scope.pageInfo = {
                    totalPages: 1,
                    totalRows: 0,
                    pageSize: 10,
                    curPage: 1
                };

                $scope.dataList4Add = [];
                $scope.pageInfo4Add = {
                    totalPages: 1,
                    totalRows: 0,
                    pageSize: 10,
                    curPage: 1
                };
                $scope.markInfo4Add = {
                    allChecked: false
                };
                $scope.condition4Add = {};
            }

            function getDataList(curPage) {
                var service = "device";
                var reqUrl = "/secondAgent/list";
                var param = {};
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageSize = $scope.pageInfo.pageSize;
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = $scope.condition;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                    });
                    $scope.markInfo.allChecked = false;

                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            /** 表格内操作开始（排序、单元格里的事件处理、行选中效果、单选、全选） */
            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {}
            };
            
            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.selectSingle = function (item) {
                if (!item.isChecked) {
                    $scope.markInfo.allChecked = false;
                } else {
                    $scope.markInfo.allChecked = checkAll($scope.dataList);
                }
            };

            $scope.selectAll = function () {
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    $scope.dataList[i].isChecked = $scope.markInfo.allChecked;
                }
            };

            function checkAll(source) {
                var selectAll = false;
                for (var i = 0, len = source.length; i < len; i++) {
                    if (!source.isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }
             /** 表格内操作结束 */


            // 新增二级代理商
            $scope.addData = function () {
                getData4Add().then(function() {
                    $scope.changeTab(1);
                })
            };


            // 删除二级代理商
            $scope.delData = function (delType, id) {
                var service = "device";
                var reqUrl = "/secondAgent/delBatch";
                var ids = [];
                if (delType === "single") {
                    ids.push(id);
                }
                if (delType === "batch") {
                    for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                        if (!!$scope.dataList[i].isChecked) {
                            ids.push($scope.dataList[i].id);
                        }
                    }
                    if (ids.length === 0) {
                        ybWidget.ShowMessage('请选择需要删除的行');
                        return;
                    }
                }
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
                    ybHttp.Post(service, reqUrl, ids).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    })
                }, function () {
                })
            };



            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    scrollLeft: { left: $scope.tbConf.fixedWidth + "px" },
                    scrollLeft4Add: { left: $scope.tbConf4Add.fixedWidth + "px" }
                };
            };

            var btnPermissionCode = {
                secondClientAdd: window.ActionCode.secondClientAdd,
                secondClientDel: window.ActionCode.secondClientDel,
            };

            $scope.btnPermission = {
                secondClientAdd: true,
                secondClientDel: true,
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }


            $scope.dataList4Add = [];

            $scope.pageInfo4Add = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            $scope.markInfo4Add = {
                allChecked: false
            };

            $scope.condition4Add = {};

            $scope.tbConf4Add = {
                fixedWidth: 40,
                scrollWidth: 1150,
                tableCol: [
                    {
                        type: "",
                        name: "企业ID",
                        width: 350,
                        dataField: "orgId",
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "公司名称",
                        width: 500,
                        dataField: "companyName",
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "公司简称",
                        width: 300,
                        dataField: "shortName",
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };

            $scope.selectSingle4Add = function (item) {
                if (!item.isChecked) {
                    $scope.markInfo.allChecked = false;
                } else {
                    $scope.markInfo.allChecked = checkAll($scope.dataList4Add);
                }
            };

            $scope.selectAll4Add = function () {
                for (var i = 0, len = $scope.dataList4Add.length; i < len; i++) {
                    $scope.dataList4Add[i].isChecked = $scope.markInfo4Add.allChecked;
                }
            };

            $scope.search4Add = function (curpage) {
                getData4Add(curpage);
            };

            function getData4Add (curPage) {
                var service = "device";
                var reqUrl = "/company/listUnBind";
                var param = {};
                if (!!curPage) {
                    $scope.pageInfo4Add.curPage = curPage;
                }

                param.pageSize = $scope.pageInfo4Add.pageSize;
                param.pageNum = $scope.pageInfo4Add.curPage;
                param.filters = {
                    shortName: $scope.condition4Add.shortName,
                    companyName: $scope.condition4Add.companyName
                };
                return ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    var data = resp.value || {};
                    $scope.pageInfo4Add.totalRows = data.total;
                    $scope.pageInfo4Add.totalPages = data.pages;
                    angular.forEach(data.list, function(item) {
                        item.isChecked = false;
                    })
                    $scope.dataList4Add = data.list;
                    return data;
                });
            }

            $scope.addSecondAgent = function () {
                var ids = [];
                for (var i = 0, len = $scope.dataList4Add.length; i < len; i++) {
                    if (!!$scope.dataList4Add[i].isChecked) {
                        ids.push({ orgId: $scope.dataList4Add[i].orgId });
                    }
                }
                if (ids.length === 0) {
                    ybWidget.ShowMessage('请选择需要添加的行');
                    return;
                }
                var service = "device";
                var reqUrl = "/secondAgent/add";
                ybHttp.Post(service, reqUrl, ids).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    getDataList();
                    $scope.changeTab(0);
                })
            };

            (function () {
                getDataList();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);  //权限码设置好后将注释放开
            })();
        }]);
})(window, window.angular);
